package com.lulin.mall.coupon.service.impl;

import com.lulin.common.dto.SkuReductionDTO;
import com.lulin.mall.coupon.entity.MemberPriceEntity;
import com.lulin.mall.coupon.entity.SkuLadderEntity;
import com.lulin.mall.coupon.service.MemberPriceService;
import com.lulin.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.Query;

import com.lulin.mall.coupon.dao.SkuFullReductionDao;
import com.lulin.mall.coupon.entity.SkuFullReductionEntity;
import com.lulin.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    //折扣
    @Autowired
    private SkuLadderService skuLadderService;

    //会员价
    @Autowired
    private MemberPriceService memberPriceService;


    /**
     * 5.3）保存满减信息、折扣，会员价等相关信息;
     * （跨系统跨库的）mall-sms:
     * <p>
     * 满减表：sms_full_reduction
     * 折扣表：sms_sku_ladder
     * 会员价表：sms_member_price
     *
     * @param dto
     */
    @Override
    public void saveFullReductionInfo(SkuReductionDTO dto) {
        //5.3) 保存满减信息。折扣、会员价
        //折扣：
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        ladderEntity.setSkuId(dto.getSkuId());
        ladderEntity.setFullCount(dto.getFullCount());
        ladderEntity.setDiscount(dto.getDiscount());
        //是优惠否叠加
        ladderEntity.setAddOther(dto.getCountStatus());
        //有可能不传，做一个简单的判断
        if (ladderEntity.getFullCount() > 0) {
            skuLadderService.save(ladderEntity);
        }

        //2、满减：
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(dto, fullReductionEntity);
        //不设置时，有空的信息，进行简单的判断,和0比较，=1 表示大于0
        if (fullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            this.save(fullReductionEntity);
        }

        //3、会员价：会员价可以有多个，因此，进行批处理
        if (dto.getMemberPrice() != null && dto.getMemberPrice().size() > 0) {
            List<MemberPriceEntity> memberPriceEntities = dto.getMemberPrice().stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(dto.getSkuId());
                // 会员等级id
                memberPriceEntity.setMemberLevelId(item.getId());
                //会员价
                memberPriceEntity.setMemberPrice(item.getPrice());
                //是否可叠加  前端没有设置，先设置可叠加
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }


}
